﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PES_TurnierManager.Model
{
    public class PlayerStatistics : IComparable<PlayerStatistics>
    {
        private uint mPlusGoals;
        private uint mMinusGoals;
        private int mGoalDifference;
        private uint mGamesPlayed;
        private uint mPoints;
        private uint mGamesWon;
        private uint mGamesLost;
        private uint mDraws;

        public PlayerStatistics()
        {
            mDraws = 0;
            mGamesLost = 0;
            mGamesPlayed = 0;
            mGamesWon = 0;
            mMinusGoals = 0;
            mPlusGoals = 0;
            mPoints = 0;
        }

        public void calculatePoints()
        {
            mPoints = mGamesWon * 3 + mDraws;
        }

        public void calculateGoalDifference()
        {
            mGoalDifference = (int)mPlusGoals - (int)mMinusGoals;
        }

        public uint getPoints()
        {
            calculatePoints();
            return mPoints;
        }

        public uint getPlusGoals()
        {
            return mPlusGoals;
        }

        public uint getMinusGoals()
        {
            return mMinusGoals;
        }

        public int getGoalDifference()
        {
            calculateGoalDifference();
            return mGoalDifference;
        }

        public uint getGamesPlayed()
        {
            return mGamesPlayed;
        }

        public uint getGamesWon()
        {
            return mGamesWon;
        }

        public uint getGamesLost()
        {
            return mGamesLost;
        }

        public void addDataOfMatch(uint goalsMarked, uint goalsGot)
        {
            mPlusGoals += goalsMarked;
            mMinusGoals += goalsGot;

            mGamesPlayed++;

            if (goalsGot > goalsMarked)
            {
                mGamesLost++;
            }
            else if (goalsMarked > goalsGot)
            {
                mGamesWon++;
            }
            else
            {
                mDraws++;
            }
        }

        public int CompareTo(PlayerStatistics otherStats)
        {
            if (otherStats.getPoints() > mPoints)
                return -1;
            else if (mPoints > otherStats.getPoints())
                return 1;

            //Points are equal. Compare the goals marked.
            if (otherStats.getPlusGoals() > mPlusGoals)
                return -1;
            else if (mPlusGoals > otherStats.getPlusGoals())
                return 1;

            //Plus goals are equal. Compare the goaldifference
            if (otherStats.getGoalDifference() > this.getGoalDifference())
                return -1;
            else if (this.getGoalDifference() > otherStats.getGoalDifference())
                return 1;

            return 0;
        }
            

    }
}
